<?php

class AccountController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout()->setLayout('index-layout');
    }


    public function getinAction()
    {
        //        $this->view->error="wrong captcha";
    }

    public function loginAction()
    {
        $usersesion = new \Zend_Session_Namespace("usersession");
        $login = $this->_getParam("UserName");
        $pas = $this->_getParam("Password");
        $udb = new \Managers\Users();

        $usr = $udb->IsRegistered($login, $pas);
        $sdb=new \Managers\Security();
        if ($usr) {
            if($this->_getParam("save-me")){
//                setcookie("user_id",$sdb->dsCrypt($usr[0]['id']),time()+3600*72,"/");
                setcookie("user_id",$sdb->dsCrypt("asdfefsf.".$usr[0]['id']),time()+3600*72,"/");
                setcookie("user_name",$sdb->dsCrypt($usr[0]['name']),time()+3600*72,"/");

                  }

            $usersesion->user_id = $usr[0]['id'];
            $usersesion->usr = $usr[0];
            $this->view->usr = $usr;
            $this->view->redirect = '<meta http-equiv="REFRESH" content="2;url=http://localhost/account/userpage"/>';
        }
        else {
            $this->_redirect(Zend_Registry::get('config')->resources->frontController->baseUrl . "account/fail/");
        }
    }

    public function logoutAction()
    {
        $usersesion = new \Zend_Session_Namespace("usersession");
        $usersesion->user_id = null;
        setcookie("user_id",null,1,"/");
        setcookie("user_name",null,1,"/");
        $this->_redirect(Zend_Registry::get('config')->resources->frontController->baseUrl . "");

    }

    public function registerAction()

    {
        //            $messagern =$this->_helper->getHelper("FlashMessager");
        //            $messagern->init();
        //
        //         $messagern->addMessage("hello dear user, how are you?");
        if ($this->_getParam('message') == 1) {
            $this->view->error = "Wrong captcha!";
        }

        if ($this->_getParam('message') == 2) {
            $this->view->error = "Wrong email!";
        }
        $captcha = new Zend_Form_Element_Captcha(
            'captcha', // This is the name of the input field
            array('label' => 'Write the chars to the field',
                 'captcha' => array( // Here comes the magic...
                     // First the type...
                     'captcha' => 'Image',
                     // Length of the word...
                     'wordLen' => 6,
                     // Captcha timeout, 5 mins
                     'timeout' => 300,
                     // What font to use...
                     'font' => '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf',
                     // Where to put the image
                     'imgDir' => 'images/captcha/',
                     // URL to the images
                     // This was bogus, here's how it should be... Sorry again :S
                     'imgUrl' => 'http://localhost/images/captcha/',
                 )));
        $this->view->captcha = $captcha->render();


    }

    public function reguserAction()
    {
        $mail = $this->_getParam('registerModel_Email');
        $pas = $this->_getParam('idregisterModel_Password');
        $captcha = $this->_getParam("captcha");
        $captchaid = $captcha['id'];
        $captchainput = $captcha['input'];

        $word = $_SESSION["Zend_Form_Captcha_" . $captchaid]['word'];

        if (strcasecmp($captchainput, $word) == 0) {

            if (preg_match('|([a-z0-9_\.\-]{1,20})@([a-z0-9\.\-]{1,20})\.([a-z]{2,4})|is', $mail) == 1) {
                $udb = new \Managers\Users();
                $this->_redirect(Zend_Registry::get('config')->resources->frontController->baseUrl . "account/userpage/" . $udb->reguser($mail, $pas));
            }
            else {
                $this->_redirect(Zend_Registry::get('config')->resources->frontController->baseUrl . "account/register/message/2");
            }
        }

        else {

            $this->_redirect(Zend_Registry::get('config')->resources->frontController->baseUrl . "account/register/message/1");
        }
    }

    public function failAction()
    {
        $this->view->redirect = '<meta http-eqsomeitems.phtmluiv="REFRESH" content="2;url=http://localhost/account/getin">';

    }

    public function userpageAction()
    {
      

        $usersesion = new \Zend_Session_Namespace("usersession");
        $bd = new \Managers\Users();
        $this->view->usr= $bd->getFirstIn($usersesion->user_id);
        $this->view->items = $bd->getItems($usersesion->user_id);

        $searchMailManager = new Managers\Mail();
        $this->view->SearchTovars = $searchMailManager->allSearchMailList($usersesion->user_id);

//        $tagsManager = new Managers\Tags();
//        $this->view->allTags = $tagsManager->getList();


    }

        public function editAction(){
        $usersesion = new \Zend_Session_Namespace("usersession");

        $db=new \Managers\Tovar();
        $tag=new \Managers\Connects();
            $this->view->tags=$tag->getTags($this->_getParam("id"));


       $urs=$db->getMaster($this->_getParam("id"));
          
            if($urs[0]['user']['id']!=$usersesion->user_id){

                $this->_redirect("http://localhost/");
            }
            else {
                $this->view->usr=$urs[0];

            }
    }
    public  function resetpasswordAction(){
        

    }




}

